import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    //重定向到登录页面
    redirect: "/login",
  },
  {
    path:'/login',
    name: '/login',//不可以重复
    //按需引入
    component: () => import('../views/Login'),
  },
  {
    path:'/home',
    name:'/home',
    redirect:'/home/index',
    component:HomeView,
    children:[
      {
        path:'index',
        name:'/home/index',
        component:()=>import('../views/Index'),
        meta:{
          breadcrumbs:['首页']
        }
      },
      {
        path:'actor-list',
        name:'/home/actor-list',
        component:()=>import('../views/actor/ActorList.vue'),
        meta:{
          breadcrumbs:['演员管理','演员列表']
        }
      },
      {
        path:'actor-add',
        name:'/home/actor-add',
        component:()=>import('../views/actor/ActorAdd.vue'),
        meta:{
          breadcrumbs:['演员管理','新增演员']
        }
      },
    ]
    
  }
]



const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//全局前置路由守卫
router.beforeEach((to,form,next)=>{
  //to 代表要去那个页面
  //form 代表你从哪个网页过来
  //next 代表放行
  if(to.path == "/login") return next();
  const token = window.sessionStorage.getItem("token")
  //如果token不存在，代表用户没有登录，代表用户非法闯入，需要通过next跳转到登录页面
  if(!token) return next("/login");
  next()
})
//全局后置路由守卫
router.afterEach((to,form)=>{})

export default router
